import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import './widgets/node.dart';

import 'size.dart';

Path dashPath(Path path, List<double> dashArray) {
  PathMetrics metrics = path.computeMetrics();

  path = Path();

  switch (dashArray.length) {
    case 0:
      dashArray.add(8);
      dashArray.add(4);
      break;

    case 1:
      dashArray.add(dashArray[0] / 2);
      break;
  }

  for (PathMetric metric in metrics) {
    bool draw = false;
    int index = 0;
    int count = dashArray.length;
    double length = metric.length;
    double distance = 0;
    double size = 0;

    while (distance < length) {
      if (index < count) {
        size = dashArray[index++];
      } else {
        index = 0;
        size = dashArray[index++];
      }

      if (draw = !draw) {
        path.addPath(
            metric.extractPath(distance, distance + size), Offset.zero);
      }

      distance += size;
    }
  }

  return path;
}

/// CSS边框样式
enum CSSBorderStyle { solid, dashed, dotted }

/// CSS边框
class CSSBorderEdge {
  CSSSize? width;

  Color? color;

  CSSBorderStyle style = CSSBorderStyle.solid;

  void paint(PaintingContext context, CSSNodeData data, Path path) {
    Paint paint = Paint()
      ..strokeWidth = width != null ? width!.compute(data, 0) : 1
      ..color = color != null ? color! : Colors.black
      ..style = PaintingStyle.stroke;

    switch (style) {
      case CSSBorderStyle.solid:
        context.canvas.drawPath(path, paint);
        break;

      case CSSBorderStyle.dashed:
        context.canvas.drawPath(dashPath(path, [8, 4]), paint);
        break;

      case CSSBorderStyle.dotted:
        context.canvas.drawPath(dashPath(path, [8, 2, 2, 2]), paint);
        break;
    }
  }
}

// CSS边框圆角
class CSSBorderRadius {}

// CSS阴影
class CSSNodeShadow {}

/// CSS边框
class CSSBorder {
  CSSBorderEdge? all;

  CSSBorderEdge? top;

  CSSBorderEdge? right;

  CSSBorderEdge? bottom;

  CSSBorderEdge? left;

  CSSBorderRadius? radius;

  CSSNodeShadow? shadow;

  void paint(PaintingContext context, CSSNodeData data) {
    Offset offset = data.offset;
    Size size = data.size;
    double x = offset.dx;
    double y = offset.dy;
    double width = size.width;
    double height = size.height;

    if (all != null) {
      Path path = Path();
      path.addRect(Rect.fromLTRB(x, y, x + width, y + height));
      all!.paint(context, data, path);
    }

    if (top != null) {
      Path path = Path();
      path.moveTo(x, y);
      path.lineTo(x + width, y);
      top!.paint(context, data, path);
    }

    if (right != null) {
      Path path = Path();
      path.moveTo(x + width, y);
      path.lineTo(x + width, y + height);
      right!.paint(context, data, path);
    }

    if (bottom != null) {
      Path path = Path();
      path.moveTo(x + width, y + height);
      path.lineTo(x, y + height);
      bottom!.paint(context, data, path);
    }

    if (left != null) {
      Path path = Path();
      path.moveTo(x, y + height);
      path.lineTo(x, y);
      left!.paint(context, data, path);
    }
  }
}
